<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <title></title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <!--app-->
        <link rel="stylesheet" href="css/fonts/font-awesome.min.css">
        <link rel="stylesheet" href="css/ui-box.css">
        <link rel="stylesheet" href="css/ui-base.css">
        <link rel="stylesheet" href="css/ui-color.css">
        <link rel="stylesheet" href="css/appcan.icon.css">
        <link rel="stylesheet" href="css/appcan.control.css">
        <link rel="stylesheet" href="css_sdk/css/main.css"> 
         <link rel="stylesheet" href="test_content/css/car.css">
        <!--amazeui-->
        <link rel="stylesheet" href="assets/css/amazeui.min.css">
        <link rel="stylesheet" href="assets/css/app.css">
        <script src="js/jquery-1.8.3.min.js"></script> 
        <style>
        
        .am-navbar-default .am-navbar-nav {
            background-color: #ffffff;
        }
        .am-navbar-default a, .am-navbar-one a {
            color: #afb7b9;
        }
        .umar-at2 {
            margin-top: 0.5em;
        }
        .am-header-default{
            background-color: #ffffff;
        }
        
        .uwh-pSC1 {
                width: 1.8em;
                height: 1.8em;
            }
        
        ol, ul {
             padding-left: 0em; 
        }
        .bc-border {
            border-color: #E2E0E0;
        }
        
        address, blockquote, dl, fieldset, figure, hr, ol, p, pre, ul {
             margin: 0 0 0rem; 
        }
        

        
        
        </style>
</head>

 <body class="um-vp bc-bg" ontouchstart>
     
    <header  data-am-widget="header" class="ub ub-ac am-header-default am-no-layout" >
        <div class="ub-f1" style="margin-left:0.5em" id="car_qty" >购物车(0)</div>
        <div class="ub-f1" style="text-align: right;">
            <button type="button" class="am-btn am-btn-danger " onclick="delAll()">删除</button>
            <button type="button" class="am-btn am-btn-danger " onclick="emptyAll()">清空购物车</button>
        </div>
    </header>
     
    
     
<!--        <div class="ub ub-ver">
            <div class="ub ub-ver c-wh umar-at2 b-gra-d6 ubt c-wh">

                <div id="listview2"  class=" bc-border sc-bg c-wh">
                     <ul>
                        <li class="ubb ub bc-border bc-text ub-ac lis" data-index="0">
                            <div class="checkbox umar-r"><input type="checkbox" class="uabs ub-con"></div>
                            <div class="lv_title ub-f1 marg-l ub ub-ver ut-m line1">
                                <div class="ub">
                                    <div>会员号：82784</div>
                                    <div class="tx-r ub-f1  t-org-c9">徐伟</div>
                                </div>   
                            </div>
                        </li>
                    </ul>
                </div>
                <div class="uinn-a7">
                   
                    <div class="uinn-a1 ub ub-ac  b-gra-d6">
                        <div class="checkbox umar-r">
                            <input type="checkbox" class="uabs ub-con">
                        </div>
                        <div class="uh-app1 uw-app2 mar-ar1 ub-img" style="background-image:url('test_content/css/myImg/scImg3.png');"></div>
                        <div class=" ub-f1 ulev-1 mar-ar1">
                            情人节礼物礼物礼物怡吻手工松露巧克力情人节...
                        </div>
                        <div class="ub ub-ver ub-ac">
                            <div class="ub-f1 ub-ac t-org-c9 ufm1 ulev-3">
                                ￥19.5
                            </div>
                            <div class="ub-f1 ub-ac sc-text ufm1 ulev-1 ">
                                x1
                            </div>
                        </div>
                    </div>
                  
                </div>
                <div id="listview2"  class="ubt ub ub-ac bc-border sc-bg c-wh" style="height:3em;padding:0.5em">
                   <div class="checkbox umar-r"><input type="checkbox" class="uabs ub-con"></div>
                   <div>使用预留库存:共8件</div>
                </div>
                
              
                
            </div>
        </div>-->
        <div id="myList" style="margin-bottom:4em">

        </div>

     
     <div data-am-widget="navbar" class="am-navbar am-cf am-navbar-default "id="" style="background-color: white">

            <div class="ub ub-ac ">
                <div class="ub ub-f1" style="padding-left:0.5em">
                    <div class="umar-mel1 umar-ar3">总数量：<span class="t-org-c9" id="total_qty">0</span></div>
                </div>
                <div class="ub-f1 ">
                    <div class="umar-mel1 umar-ar3">合计：<span class="t-org-c9" id="total_amount">￥0</span></div>
                </div>
                
                <button onclick="click_sub()" type="button" class="am-btn am-btn-danger " style="padding: .9em 1.1em;">结算</button>
              
             </div>

     </div>
     
      <script>
        var user=localStorage.getItem("user");//用户
        var product_list=[];//商品id列表
        var virtual_list=[];//虚拟商品列表
         //虚拟商品列表
        //库存
        var inventoryList;
        function virtual_data(){
            $.ajax({
                type: 'POST',
                url: "http://101.200.158.44:80/CosmeticSunyuxiWebService/services/CaigouManageWebService/GetVirtualQty" ,
                data: {"ID":user},
                success: function(data){
                    inventoryList=data;
                },
                dataType: "json",
            });                 
        }
        virtual_data();
        function getInventory(catID){
            var ret=0;
            $.each(inventoryList[0]["virList"],function(idx,item){
                if(item.f1==catID){
                    ret = item.f3;
                }
             });
             return ret;
        }
         function getInventory_xu(catID){
            var ret=0;
            $.each(inventoryList[0]["virList"],function(idx,item){
                if(item.f2==catID){
                    ret = item.f3;
                }
             });
             return ret;
        }
        //购物车列表
        function car_list(){
//            var user=sessionStorage.getItem("user");
            var ullis = [];
            var itemLis=[];
            $.ajax({
                type: 'POST',
                url: "http://101.200.158.44:80/CosmeticSunyuxiWebService/services/CaigouManageWebService/getCartInit" ,
                data: {"ID":user},
                success: function(data){
                    var group_member=null;//会员
                    var group_type=null;//商品类别
                    //购物车数量
                     var qty=data[0]["productQty"];
                     $("#car_qty").html('购物车('+qty+')');
                    //数据列表
                    var arrCartDetail=eval(data[0]["carDetail"]);
                    //购物车数量
                    if(arrCartDetail.length <= 0){
                            return;
                    }         
                    for(var i=0;i<=arrCartDetail.length;i++){ 
                        if(i==arrCartDetail.length){
                            if(itemLis.length>0)
                            {
                                    ullis.push(itemLis.join('')); 
                                    ullis.push('<div id="listview2"  class=" ub ub-ac bc-border sc-bg c-wh" style="padding:0.5em">');
//                                  ullis.push('<div class="checkbox umar-r"><input type="checkbox" onclick="chcek_all(\'check_f9_'+group_member+'_'+group_type+'\')" id="check_f9_'+group_member+'_'+group_type+'" name="check_f9_'+group_member+'_'+group_type+'" class="uabs ub-con"></div>');
//                                  ullis.push('<div>使用预留库存:共'+getInventory_xu(arrCartDetail[i-1].f4)+'件</div>');
                                    ullis.push('</div>');
                                    ullis.push('</div>');
                                    ullis.push('</div>');
                                    itemLis=[];
                                    break;
                            } 
                        }
                        if(arrCartDetail[i].f1!=group_member||arrCartDetail[i].f10!=group_type){
                            if(itemLis.length>0)
                            {
                                    ullis.push(itemLis.join('')); 
                                    ullis.push('<div id="listview2"  class=" ub ub-ac bc-border sc-bg c-wh" style="padding:0.3em">');
//                                  ullis.push('<div class="checkbox umar-r"><input type="checkbox" onclick="chcek_all(\'check_f9_'+group_member+'_'+group_type+'\')" id="check_f9_'+group_member+'_'+group_type+'" name="check_f9_'+group_member+'_'+group_type+'" class="uabs ub-con"></div>');
//                                  ullis.push('<div>使用预留库存:共'+getInventory(arrCartDetail[i-1].f10)+'件</div>');
                                    ullis.push('</div>');
                                    ullis.push('</div>');
                                    ullis.push('</div>');
                                    itemLis=[];
                            }                           
                           ullis.push('<div class="ub ub-ver">'); 
                           ullis.push('<div class="ub ub-ver c-wh umar-at2 b-gra-d6 ubt c-wh">');
                           ullis.push('<div id="listview2"  class=" bc-border sc-bg c-wh">');
                           ullis.push('<ul>');
                           ullis.push('<li class="ubb ub bc-border bc-text ub-ac lis" data-index="0">');
                           ullis.push('<div class="checkbox umar-r"><input type="checkbox" onclick="chcek_all(\'check_'+arrCartDetail[i].f1+'_'+arrCartDetail[i].f10+'\')" id="check_'+arrCartDetail[i].f1+'_'+arrCartDetail[i].f10+'" class="uabs ub-con"></div>');
                           ullis.push('<div class="lv_title ub-f1 marg-l ub ub-ver ut-m line1">');
                           ullis.push('<div class="ub">');
                           ullis.push('<div>会员号：'+arrCartDetail[i].f1+'</div>');
                           ullis.push('<div class="tx-r ub-f1  t-org-c9">'+arrCartDetail[i].f2+'</div>');
                           ullis.push('</div>');
                           ullis.push('</div>');
                           ullis.push('</li>');
                           ullis.push('</ul>');
                           ullis.push('</div>');
                        }
                        itemLis.push('<div class="uinn-a7" style="margin-top:1em">');
                        itemLis.push('<div class="uinn-a1 ub ub-ac  b-gra-d6">');
                        itemLis.push('<div class="checkbox umar-r">');
                        //通过会员id品类id商品cd组成新的id
                        var str_id=arrCartDetail[i].f1+'_'+arrCartDetail[i].f10+'_'+arrCartDetail[i].f4;
                        //设置id
                        itemLis.push('<input type="checkbox" onclick="get_total()" id="id_'+str_id+'" name="check_'+arrCartDetail[i].f1+'_'+arrCartDetail[i].f10+'"  class="uabs ub-con">');
                        product_list.push(str_id);
                        //隐藏数据
                        itemLis.push('<div style="display:none;">');
                        itemLis.push('<input placeholder="会员ID" id="member_'+str_id+'"  value="'+arrCartDetail[i].f1+'" type="text">');
                        itemLis.push('<input placeholder="品类ID" id="type_'+str_id+'"  value="'+arrCartDetail[i].f10+'" type="text">');
                        itemLis.push('<input placeholder="商品CD" id="product_'+str_id+'"  value="'+arrCartDetail[i].f4+'" type="text">');
                        itemLis.push('<input placeholder="加入购物车数量" id="num_'+str_id+'"  value="'+arrCartDetail[i].f8+'" type="text">');
                        itemLis.push('<input placeholder="卖价" id="amount_'+str_id+'"  value="'+arrCartDetail[i].f7+'" type="text">');
                        itemLis.push('<input placeholder="最小数量" id="min_'+str_id+'"  value="'+arrCartDetail[i].f12+'" type="text">');
                        itemLis.push('<input id="check_f9_'+str_id+'"name="check_f9_'+arrCartDetail[i].f1+'_'+arrCartDetail[i].f10+'"class="uabs ub-con" type="checkbox">');
                        itemLis.push('</div>');
                        //
                        itemLis.push('</div>');
                        itemLis.push('<div class="uh-app1 uw-app2 mar-ar1 ub-img" style="background-image:url(http://101.200.158.44/SyxView/UpImages/Product/'+arrCartDetail[i].f3+');"></div>');
                        itemLis.push('<div class=" ub-f1 ulev-1 mar-ar1">');
                        itemLis.push(arrCartDetail[i].f5);
                        itemLis.push('</div>');
                        itemLis.push('<div class="ub ub-ver ub-ac">');
                        itemLis.push('<div class="ub-f1 ub-ac t-org-c9 ufm1 ulev-3">');
                        itemLis.push('X'+arrCartDetail[i].f8);
                        itemLis.push('</div>');
//                        itemLis.push('<div class="ub-f1 ub-ac sc-text ufm1 ulev-1 ">');
//                        itemLis.push('x'+arrCartDetail[i].f8);
//                        itemLis.push('</div>');
                        itemLis.push('<div class="ub-f1 ub-ac sc-text ufm1 ulev-1 ">');
                        itemLis.push('</div>');
                        itemLis.push('</div>');
                        itemLis.push('</div>');
                        itemLis.push('<div style="text-align:right;font-size:0.5em;margin-bottom:1em">预留：'+getInventory_xu(arrCartDetail[i].f4)+'件</div>');
                        itemLis.push('<div class="ub ub-pe ub-f1 ub-pc " style="color: gray">本次使用预留库存：<div onclick="del(\'ids_'+str_id+'\')" class=" uwh-pSC1 tx-c uba bc-border uc-a-app1 t-bla mar-ar1">-</div> <div class="uba uc-a-app1 bc-border uinput uwh-pSC1 mar-ar1 c-wh"><div id="ids_'+str_id+'" placeholder="2" type="text" class="uinn-pSC3 t-bla tx-c">0</div></div><div onclick="add(\'ids_'+str_id+'\')" class="ub ub-pc ub-ac uwh-pSC1 tx-c uba bc-border uc-a-app1 t-bla"><div>+</div></div> </div>');
                        itemLis.push('<input type="hidden" id="yids_'+str_id+'" value="'+getInventory_xu(arrCartDetail[i].f4)+'"><div class="ubb bc-border" style="margin-top:1em;margin-left:3em"></div></div>');
                        group_member=arrCartDetail[i].f1;
                        group_type=arrCartDetail[i].f10;
                    } 
//                    if(itemLis.length>0)
//                    {
//                        ullis.push(itemLis.join(''));
//                        ullis.push("</div>");
//                        ullis.push("</div>");
//                        itemLis=[];
//                    }    
                    $("#myList").empty().html(ullis.join(''));
                    //id列表打印
                    for(var i=0;i<product_list.length;i++){
                        console.log(product_list[i]);
                    } 
                },
                dataType: "json",
            });                
        }
        car_list();
        //结算
        function click_sub(){
            var data='';
            var sum=0;
            var is_first=true;
            var product_type=0;
            var member='';   
            var min_num=0;
            for(var i=0;i<product_list.length;i++){
                var member_id=$("#member_"+product_list[i]).val(); //会员id
                var type_id=$("#type_"+product_list[i]).val();//类别id
                var product_id=$("#product_"+product_list[i]).val();//商品id
                var qty=$("#num_"+product_list[i]).val();//数量
                min_num=$("#min_"+product_list[i]).val();//最小数量
                var check_f9=0;//是否使用虚拟商品
                var collid = document.getElementById("id_"+product_list[i]);               
                if (collid.checked){
                    if (is_first){
                        member=member_id;
                        product_type=type_id;
                        is_first=false;
                    }
                    //判断商品购买数量是否满足
                    if(product_type==type_id&&member==member_id){
                        sum+=parseInt(qty);
                    }else{
                        if(sum%min_num!=0){
                            alert('会员：'+member+'购买数量不够'+Math.ceil(sum/min_num)+'套！');
                            return;                       
                        }                          
//                        if(min_num>sum){
//                            alert('会员：'+member+'购买数量不够！');
//                            return;
//                        }
                        product_type=type_id;
                        member=member_id;
                        sum=parseInt(qty);
                    }
//                    var collid_f9 = document.getElementById("check_f9_"+product_list[i]);
//                    if(collid_f9.checked){
//                        check_f9=1;
//                    }
                    
                     var yids = document.getElementById("yids_"+product_list[i]).value;
                     var collid_f9 = document.getElementById("ids_"+product_list[i]).innerHTML;
                     var ynum='';
                     if(collid_f9>0){
                         check_f9=1;
                         ynum=","+collid_f9;
                     }
                     
                     if(collid_f9>yids){
                         alert("预存数量不足，请检查您的订单。");
                         return;
                     }

                    //组参数
                    if(data==''){
                        data=user+','+member_id+','+type_id+','+product_id+','+qty+','+check_f9+ynum;
                    }else{
                        data+='$'+user+','+member_id+','+type_id+','+product_id+','+qty+','+check_f9+ynum;
                    }
                      
                }
//                alert(sum);
            }
            if(sum!=0){
                if(sum%min_num!=0){
                    alert('会员：'+member+'购买数量不够'+Math.ceil(sum/min_num)+'套！');
                    return;                       
                }             
            }
            if(data!=''){
                $.ajax({
                    type: 'POST',
                    url: "http://101.200.158.44:80/CosmeticSunyuxiWebService/services/CaigouManageWebService/SaveOrderInfo" ,
                    data: {"jsonData":data},
                    success: function(data){
                        if(data==false || data[0]["ConfirmDetail"].length==0){
                            alert('结算失败！');
                            return;
                        }
                        var arrConfirm=eval(data[0]["ConfirmDetail"]);
                        var order_ids=[];
                        var address='';
                        for(var i=0;i<arrConfirm.length;i++){ 
                            order_ids.push(arrConfirm[i].f4);
                            address=arrConfirm[i].f11;
                        }
                        sessionStorage.setItem("order_ids", order_ids);
                        sessionStorage.setItem("address", address);
                        window.location.href="order_right.html";
                    },
                    dataType: "json",
                });                
            }else{
                alert('请选择商品！')
            }
            console.log(data);
        }
        //全选
        function chcek_all(vlaue){
            var collid = document.getElementById(vlaue);
            var coll = document.getElementsByName(vlaue);
            if (collid.checked){
              for(var i = 0; i < coll.length; i++)
                coll[i].checked = true;
           }else{
              for(var i = 0; i < coll.length; i++)
                coll[i].checked = false;
           }
           //计算合计
           get_total();
        }
        //
        function get_total(){
            var total_qty=0;//总数量
            var total_amount=0;//总金额
            for(var i=0;i<product_list.length;i++){
                var collid = document.getElementById("id_"+product_list[i]);
                if (collid.checked){
                    var qty=$("#num_"+product_list[i]).val();//数量
                    total_qty+=parseInt(qty);
                    var amount=$("#amount_"+product_list[i]).val();//卖价
                    amount=amount*qty;
                    total_amount+=amount;
                }
            }
            $("#total_qty").html(total_qty);
            $("#total_amount").html('￥'+total_amount);
        }
        //删除
        function delAll(){
            var is_delete=false;
//            var is_success=false;
            for(var i=0;i<product_list.length;i++){
                var collid = document.getElementById("id_"+product_list[i]);
                if (collid.checked){
                    is_delete=true;
                    var member_id=$("#member_"+product_list[i]).val(); //会员id
                    var product_id=$("#product_"+product_list[i]).val();//商品id
                    $.ajax({
                        type: 'POST',
                        url: "http://101.200.158.44:80/CosmeticSunyuxiWebService/services/CaigouManageWebService/DelCartInfo" ,
                        data: {"ID":user,"UserID":member_id,"ProductCD":product_id},
                        success: function(data){    
                            var result=data[0]["Result"];
                            if(result=="true"){
//                                is_success=true;
                                window.location.reload();
                            }else{
                                altert("删除失败");
                                window.location.reload();
                            }
                        },
                        dataType: "json",
                    });                    
                    
                }
            }
//            if (is_success==true){
//                alert(is_success);
//                window.location.reload();
//            }
            if(!is_delete){
                alert("请选择删除商品");
            }
        }
        //清空购物车
        function emptyAll(){
//            var is_success=false;
            for(var i=0;i<product_list.length;i++){
                var member_id=$("#member_"+product_list[i]).val(); //会员id
                var product_id=$("#product_"+product_list[i]).val();//商品id
                $.ajax({
                    type: 'POST',
                    url: "http://101.200.158.44:80/CosmeticSunyuxiWebService/services/CaigouManageWebService/DelCartInfo" ,
                    data: {"ID":user,"UserID":member_id,"ProductCD":product_id},
                    success: function(data){    
                        var result=data[0]["Result"];
                        if(result=="true"){
//                            is_success=true;
                            window.location.reload();
                        }else{
                            altert("清空失败");
                            window.location.reload();
                        }
                    },
                    dataType: "json",
                });                  
            }       
//            if(is_success){
//                window.location.reload();
//            }
        }
        
        function add(id){
			$("#"+id).text(parseInt($("#"+id).text())+1);
	}
        
        function del(id){
              
			if(parseInt($("#"+id).text())>0){
				$("#"+id).text(parseInt($("#"+id).text())-1);

			}
	}
        
    </script>
    </body>
</html>
